package com.asset.dispoal.dataDetail.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.dataDetail.domain.db.entity.SysDataDetailTemplate;
import com.asset.dispoal.dataDetail.domain.db.mapper.SysDataDetailMapper;
import com.asset.dispoal.dataDetail.dto.SysDataDetailTemplateRequest;
import com.asset.dispoal.dataDetail.dto.SysDataDetailTemplateResponse;
import com.asset.dispoal.dataDetail.service.converter.SysDataDetailConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/23$
 */
@Component
public class DataDetailTemplateQueryPageCommand implements BaseCommand<SysDataDetailTemplateRequest, Result<PageVO<SysDataDetailTemplateResponse>>> {

    @Autowired
    SysDataDetailMapper sysDataDetailMapper;

    @Override
    public Result<PageVO<SysDataDetailTemplateResponse>> execute(SysDataDetailTemplateRequest request) {
        SysDataDetailTemplate sysDataDetail = SysDataDetailConverter.INSTANCE.reqTemplateToDo(request);
        int count = sysDataDetailMapper.countTemplate(sysDataDetail);
        Page<SysDataDetailTemplate> page = PagingQuery.getPage(count, sysDataDetail);
        PageVO<SysDataDetailTemplateResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<SysDataDetailTemplate> dataDetails = sysDataDetailMapper.pageTemplate(page);
            final List<SysDataDetailTemplateResponse> sysDataDetailResponses = SysDataDetailConverter.INSTANCE.doTemplateToRes(dataDetails);
            pageVO.setResult(sysDataDetailResponses);
        }
        return Result.ok(pageVO);
    }
}
